from tortoise import fields
from .base import TimestampMixin

class QuestionType:
    SINGLE_CHOICE = "single_choice"  # 单选题
    MULTIPLE_CHOICE = "multiple_choice"  # 多选题
    TRUE_FALSE = "true_false"  # 判断题
    FILL_BLANK = "fill_blank"  # 填空题
    SHORT_ANSWER = "short_answer"  # 简答题

class DifficultyLevel:
    EASY = "easy"
    MEDIUM = "medium"
    HARD = "hard"

class QuestionStatus:
    DRAFT = "draft"  # AI生成的草稿题目
    PUBLISHED = "published"  # 教师确认的正式题目

class Question(TimestampMixin):
    """题目模型"""
    id = fields.IntField(pk=True)
    subject = fields.ForeignKeyField(
        'models.Subject', related_name='questions', description="所属学科"
    )
    title = fields.TextField(description="题目标题")
    content = fields.TextField(description="题目内容")
    type = fields.CharField(max_length=20, description="题目类型")
    difficulty = fields.CharField(max_length=10, description="难度等级")
    answer = fields.JSONField(description="答案", null=True)
    analysis = fields.TextField(description="解析")
    reference = fields.TextField(description="参考引用")
    status = fields.CharField(
        max_length=20, 
        description="题目状态",
        default=QuestionStatus.DRAFT
    )
    chapter = fields.ForeignKeyField(
        'models.Chapter', 
        related_name='questions', 
        description="所属章节"
    )
    
    # 添加填空题空位数量字段
    blank_count = fields.IntField(description="填空题空位数量", null=True)
    
    # 添加标准答案字段，用于简答题
    standard_answer = fields.TextField(description="标准答案", null=True)
    
    # 添加答案要点字段，用于简答题评分标准
    answer_points = fields.JSONField(description="答案要点", null=True)
    
    class Meta:
        table = "questions"

class QuestionSource(TimestampMixin):
    """题目来源片段模型"""
    id = fields.IntField(pk=True)
    question = fields.ForeignKeyField(
        'models.Question', related_name='sources', description="关联题目"
    )
    chapter_content = fields.ForeignKeyField(
        'models.ChapterContent', 
        related_name='question_sources', 
        description="来源章节内容"
    )
    content = fields.TextField(description="原文内容")
    position = fields.JSONField(description="内容位置信息")
    
    class Meta:
        table = "question_sources"

class QuestionOption(TimestampMixin):
    """题目选项模型"""
    id = fields.IntField(pk=True)
    question = fields.ForeignKeyField(
        'models.Question', 
        related_name='options', 
        description="关联题目"
    )
    option_label = fields.CharField(max_length=10, description="选项标签，如 A、B、C、D")
    content = fields.TextField(description="选项内容")
    is_correct = fields.BooleanField(description="是否为正确选项", default=False)
    order = fields.IntField(description="选项排序", default=0)
    
    class Meta:
        table = "question_options" 